﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Delay;
namespace PhoneInfo.Page
{
    public partial class Collectionxaml : PhoneApplicationPage
    {
        ObservableCollection<CollectTable> _list;        
        public Collectionxaml()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //读取数据库中的内容
            _list = new ObservableCollection<CollectTable>(queryCollect());
            Collect.DataContext = _list;

        }
        public static List<CollectTable> queryCollect()
        {
            var CollectInDB = (from CollectTable Item in App.ManagerDB.collectTable
                            
                            select Item).ToList();
            return CollectInDB;
        }

        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender==null)
            {
                return;
            }
            ListBox lb = sender as ListBox;
            if(lb==null)
            {
                return;
            }
            CollectTable item = lb.SelectedItem as CollectTable;
            if (item==null)
            {
                return;
            }
            string objective = "/Page/ContentAndReply.xaml";
            if (item.Topicid == null)
            {
                ToastPrompt toastPrompt = new ToastPrompt()
                {
                    Message = "帖子ID为空，无法跳转到收藏"
                };
                toastPrompt.Show();
                return;

            }
            objective += "?topicid=" + item.Topicid;
            objective += "&readcount=" + item.Readcount;
            objective += "&replycount=" + item.Replycount;
            objective += "&title=" + item.Title;
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }

        private void DeleteCollection(object sender, RoutedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            MenuItem Menuitem = (MenuItem)sender;
            CollectTable currentItem;
            currentItem = (CollectTable)Menuitem.DataContext;
            deleteaMyReadCityItem(currentItem);

        }
        private  void deleteaMyReadCityItem(CollectTable deleteItem)
        {


            foreach (var item in _list)
            {
                if (item.Topicid == deleteItem.Topicid)
                {

                    _list.Remove(deleteItem);                   
                    App.ManagerDB.collectTable.DeleteOnSubmit(item);
                    App.ManagerDB.SubmitChanges();
                    //Collect.DataContext = list;
                    break;
                }
            }
        }
    }
}